<?php
	/**<!--
		=================================================================================================
		Copyright (c) 2012 Athena Health. 
		This software is the proprietary information of Athena Health
		All Right Reserved.
		Repository path:    $HeadURL: http://athena-call-routing.googlecode.com/svn/trunk/Login.class.php $
		Last committed:     $Revision: 45 $
		Last changed by:    $Author: davidjmorin@gmail.com $
		Last changed date:    $Date: 2012-07-02 21:10:12 -0400 (Mon, 02 Jul 2012) $
		ID:            $Id: index.php 45 2012-07-03 01:10:12Z davidjmorin@gmail.com $
		=================================================================================================
	-->**/
	
	// username 
	
	define('LOGIN_USER', "dave");
	// password  
	
	define('LOGIN_PASS', "test");
	class Login {
		
		
		var $prefix = "login1_";
		
		// days cookies will remain
		var $cookie_duration = 21;
		
		
		var $user = "";
		var $pass = "";
		function authorize() {
			
			
			if(isset($_COOKIE[$this->prefix.'user'])){
				$_SESSION[$this->prefix.'user'] = $_COOKIE[$this->prefix.'user'];
				$_SESSION[$this->prefix.'pass'] = $_COOKIE[$this->prefix.'pass'];
			}
			
			
			if(isset($_POST['action']) && $_POST['action'] == "set_login"){
				
				$this->user = $_POST['user'];
				$this->pass = md5($this->prefix.$_POST['pass']); 
				
				$this->check();
				
				if(isset($_POST['remember'])){
					setcookie($this->prefix."user", $this->user, time()+($this->cookie_duration*86400));// (d*24h*60m*60s)
					setcookie($this->prefix."pass", $this->pass, time()+($this->cookie_duration*86400));// (d*24h*60m*60s)
				}
				
				//set session (dm)
				$_SESSION[$this->prefix.'user'] = $this->user;
				$_SESSION[$this->prefix.'pass'] = $this->pass;
			}
			
			
			elseif(isset($_GET['action']) && $_GET['action'] == "prompt"){
				session_unset();
				session_destroy();
				
				if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25));
				if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25));
				
				$this->prompt();
			}
			
			elseif(isset($_GET['action']) && $_GET['action'] == "clear_login"){
				session_unset();
				session_destroy();
				
				//destroy any existing cookie by setting time in past (DM_)
				if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25));
				if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25));
				
				$msg = '<h2 class="msg">**Logout complete**</h2>';
				$this->prompt($msg);
			}
		
		
		elseif(!isset($_SESSION[$this->prefix.'pass']) || !isset($_SESSION[$this->prefix.'user'])){
		$this->prompt();
		}
		
		else{
		$this->user = $_SESSION[$this->prefix.'user'];
		$this->pass = $_SESSION[$this->prefix.'pass'];
		$this->check();//dies if incorrect
		}
		
		}
		
		//////desc: compares the user info
		
		function check(){
		
		if(md5($this->prefix . LOGIN_PASS) != $this->pass || LOGIN_USER != $this->user){
		///Destroy existing cookies
		if(!empty($_COOKIE[$this->prefix.'user'])) setcookie($this->prefix."user", "blanked", time()-(3600*25));
		if(!empty($_COOKIE[$this->prefix.'pass'])) setcookie($this->prefix."pass", "blanked", time()-(3600*25));
		session_unset();
		session_destroy();
		
		$msg='<h2 class="warn">Incorrect username or password</h2>';
		$this->prompt($msg);
		}
		}
		function prompt($msg=''){
		?>
		<html><head>
		<title>Login</title>
		<style>
		body{margin:15px;}
		table.login{border-collapse:collapse;}
		table.login td{font:bold 10pt verdana;color:#f0cb01;border:1px #535353 solid;border-collapse:collapse;padding:2px 3px;text-align:center;background:#576c11;}
		table.login td.header{background: URL(http://www.athenahealth.com/_img/boxes/carousel_bg.png);}
		.msg{font:bold 120% verdana;text-align:center;color:#f0cb01;}
		.warn{font:bold 120% verdana;text-align:center;color:#f0cb01;}
		</style>
		<style type="text/css">
		body {background-color:#42520e; color: #f0cb01;
		
		}
		th {background: URL(http://www.athenahealth.com/_img/boxes/carousel_bg.png);}
		
		p {color:blue;}
		a:link {color: #f0cb01; text-decoration: underline; }
		a:active {color: #f0cb01; text-decoration: underline; }
		a:visited {color: #f0cb01; text-decoration: underline; }
		a:hover {color: #f0cb01; text-decoration: none; }
		</style>
		</head><body>
		<center><img src="http://a0.twimg.com/profile_images/1138695863/twitter-icon.png" width="100px"></center>
		<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" target="foo" onSubmit="window.open('', 'foo', 'width=1040,height=900,status=yes,resizable=yes,scrollbars=yes'); document.location='success.php'">
		<input type="hidden" name="action" value="set_login">
		
		<?php echo $msg; ?>
		
		<table align="center" width="300" class="login">
		<tr><td class="header" colspan="2">Login</td></tr>
		<tr>
		<td class="desc"><label for="user">Username:</label> <input type="text" name="user" id="user"></td>
		<td class="desc"><label for="pass">Password:</label> <input type="password" name="pass" id="pass"></td>
		</tr>
		
		<tr><td class="desc" colspan="2" style="text-align:left;">
		<input type="checkbox" name="remember" id="remember"> <label for="remember">Remember me on this computer</label>
		</td></tr>
		
		<tr><td class="desc" colspan="2" style="text-align:right;"><input type="submit" value="Login"></td></tr>
		<center> The login username is Athena and the password is the same password as the training computer.
		</table>
		
		</form>
		</body></html>
		<?php
		
		exit;
		}
		
		
		}
		
		?>					